{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5fbe2a8e",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-11-8\n",
    "* 课程：Python语言\n",
    "* week08(10周)\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "363e91fd",
   "metadata": {},
   "source": [
    "# 1. 本周内容\n",
    "\n",
    "## 1. 列表的数据处理（列表的方法）\n",
    "\n",
    "> 1. 查寻数据\n",
    ">> 1. index() 按照索引值查寻数据，并取出值\n",
    ">> 2. slice 切片方法取值\n",
    ">> 3. in 方法查询元素是否存在\n",
    "> 2. 增加数据\n",
    ">> 1. append() 增加在末尾\n",
    ">> 2. insert() 指定位置增加\n",
    "> 3. 删除数据\n",
    ">> 1. pop() 指定位置删除\n",
    ">> 2. remove() 指定结果（值）删除\n",
    ">> 3. clear() 清空列表\n",
    "> 4. 修改数据\n",
    ">> 1. 找到index进行修改\n",
    "> 5. 其他方法\n",
    ">> 1. 排序：sort() reverse()\n",
    ">> 2. 复制：copy() \n",
    "\n",
    "## 2. 元组\n",
    "\n",
    "## 3. 列表实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78cc2f7c",
   "metadata": {},
   "source": [
    "## 1.1 列表的删除 \n",
    "\n",
    "> 1. pop() \n",
    ">> 1. 默认删除列表中最后一位数据\n",
    ">> 2. 指定位置进行删除\n",
    ">> 3. 注意事项：避免indexerror的出现，先检查列表长度：len()\n",
    "> 2. remove()\n",
    ">> 1. 指定结果（值）删除\n",
    ">> 2. 注意事项：避免valueerror的出现，先检查元素是否存在 in\n",
    "> 3. clear() "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e05cf07f",
   "metadata": {},
   "source": [
    "### 1. pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "632e26b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "247f7c2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3bdebf19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李晓明'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9b84016b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李晓明'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c9835499",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b7c0197b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c63858a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '小红']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8f94f373",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先检查列表长度\n",
    "len(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4f065a6f",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "pop index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-19-47237730833d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mname_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m: pop index out of range"
     ]
    }
   ],
   "source": [
    "name_list.pop(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6251f847",
   "metadata": {},
   "source": [
    "### 2. remove()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5ad80ae5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4bd9456b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "e16b1130",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.remove(\"小红\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "dd0a6d56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '李晓明']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "02eb7c63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先查询是否存在该值\n",
    "\"小红\" in name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3ffd3434",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-3e14bc630316>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mname_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"小红\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "name_list.remove(\"小红\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a17ef5d",
   "metadata": {},
   "source": [
    "### 3. clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e7693be1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2805b75a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function clear:\n",
      "\n",
      "clear() method of builtins.list instance\n",
      "    Remove all items from list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b9e85902",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9aff36ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a0d1325",
   "metadata": {},
   "source": [
    "## 2. 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8424fbca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 17, 19, 20, 21]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list = [18,19,18,17,19,20,21]\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e6c1b63b",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b8e78280",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[17, 18, 18, 19, 19, 20, 21]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "856e4a1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4efe883e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[21, 20, 19, 19, 18, 18, 17]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4faf87c4",
   "metadata": {},
   "source": [
    "## 3. 复制 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c95bf1e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n",
      "2564442262720\n"
     ]
    }
   ],
   "source": [
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)\n",
    "print(id(name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "0d616a01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n",
      "2564442262720\n"
     ]
    }
   ],
   "source": [
    "存档_name_list = name_list\n",
    "print(存档_name_list)\n",
    "print(id(存档_name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "37edd3a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"Jalex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "5771d27b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明', 'Jalex']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "40bfa8fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明', 'Jalex']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 请问：存档_name_list 的结果会不会发生变化？\n",
    "存档_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "edfde125",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n",
      "2564440622976\n"
     ]
    }
   ],
   "source": [
    "# 综上，并不能真正的复制，接下来我们看一下copy\n",
    "name_list = [\"小明\",\"张三\",\"小红\",\"李晓明\"]\n",
    "print(name_list)\n",
    "print(id(name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "a5aa7f1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', '张三', '小红', '李晓明']\n",
      "2564437560768\n"
     ]
    }
   ],
   "source": [
    "copy_name_list = name_list.copy()\n",
    "print(copy_name_list)\n",
    "print(id(copy_name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "8ed69d5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"Jalex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "5b3c62a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明', 'Jalex']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "22dc158d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '张三', '小红', '李晓明']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "copy_name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfdac734",
   "metadata": {},
   "source": [
    "# 2. 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "e5838615",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = 12345, 54321, 'hello!'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "f225790d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12345, 54321, 'hello!')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(t)\n",
    "print(type(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "0a5e6e3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321, 'hello!')"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (12345, 54321, 'hello!')\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "c9788afc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "0aee1351",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12345, 54321)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "f7e3d687",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.index(54321)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "00671973",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t.count(12345)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "69bf74f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2023, tm_mon=11, tm_mday=8, tm_hour=11, tm_min=31, tm_sec=16, tm_wday=2, tm_yday=312, tm_isdst=0)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 案例：\n",
    "import time\n",
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "b1ee331f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1699414337.6558506"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.time()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34a71efb",
   "metadata": {},
   "source": [
    "# 实践："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "807a34ff",
   "metadata": {},
   "source": [
    "　　购物车程序项目：\n",
    "```\n",
    "　　要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "　　   　　2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "　　  　　 3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "　　　　   4、用户可以随时退出购买，退出时打印用户已购买的商品和支付宝余额。\n",
    "```\n",
    "\n",
    "```\n",
    "1. alipay = input()\n",
    "\n",
    "商品列表 = [\n",
    "\n",
    "]\n",
    "购物车 = []\n",
    "\n",
    "print(商品列表)\n",
    "\n",
    "2. user_choose = input()\n",
    "\n",
    "if 商品价格 > alipay \n",
    "    1.\n",
    "else:\n",
    "    2.\n",
    "\n",
    "3. 购物车.append（购买到的商品）\n",
    "\n",
    "4. \n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c79747ee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
